package com.xywl.escort.view.notice;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.xywl.escort.R;
import com.xywl.escort.adapter.NoticeListAdapter;
import com.xywl.escort.base.BaseActivity;
import com.xywl.escort.bean.NoticeResp;
import com.xywl.escort.bean.NoticeResp.DataBean.ListBean;
import com.xywl.escort.utils.RxSPTool;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import dagger.android.AndroidInjection;

import static com.xywl.escort.common.PreferenceKeys.USER_ID;

@Route(path = "/xy/notice/list")
public class NoticeListActivity extends BaseActivity implements NoticeListContract.View, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.progress_bar)
    ProgressBar mProgressBar;
    @BindView(R.id.recycler_notice)
    RecyclerView mRecyclerNotice;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private NoticeListAdapter mAdapter;
    private List<ListBean> mList = new ArrayList<>();

    private int pageNum = 1;
    private boolean isEnd = false;

    @Inject
    NoticeListContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_list);
        ButterKnife.bind(this);

        showBackButton();
        setTitleText(R.string.notice);

        initView();
        initData();
        addListener();
    }

    private void initView() {
        mAdapter = new NoticeListAdapter(R.layout.item_notice_list, mList);
        mRecyclerNotice.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerNotice.setAdapter(mAdapter);

        View emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty_view, null);
        TextView tvEmpty = emptyView.findViewById(R.id.tv_empty);
        tvEmpty.setText("暂无公告信息");
        mAdapter.setEmptyView(emptyView);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.aid_prompt_color, R.color.colorBackground);
    }

    private void initData() {
        pageNum = 1;
        mPresenter.takeView(this);
        final String userId = RxSPTool.getString(this, USER_ID);
        mPresenter.loadNoticeList(userId);

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mSwipeRefreshLayout.setEnabled(false);
                mRecyclerNotice.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeRefreshLayout.setEnabled(false);
                        if (isEnd) {
                            //数据全部加载完毕
                            mAdapter.loadMoreEnd();
                            mSwipeRefreshLayout.setEnabled(true);
                        } else {
                            //成功获取更多数据
                            mPresenter.loadNoticeList(userId);
                        }
                    }
                }, 200);
            }
        }, mRecyclerNotice);
    }

    private void addListener() {
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ListBean bean = mList.get(position);
                String title = bean.title;
                String content = bean.content;
                String time = bean.onlineTime;
                ARouter.getInstance()
                        .build("/xy/notice/detail")
                        .withString("title", title)
                        .withString("content", content)
                        .withString("time", time)
                        .navigation();
            }
        });
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.<T>bindToLifecycle();
    }

    @Override
    public void onRefresh() {
        mAdapter.setEnableLoadMore(false);
        pageNum = 1;
        String userId = RxSPTool.getString(this, USER_ID);
        mPresenter.loadNoticeList(userId);
    }

    @Override
    public void updateNoticeList(NoticeResp.DataBean dataBean) {
        mProgressBar.setVisibility(View.GONE);
        mSwipeRefreshLayout.setRefreshing(false);
        mRecyclerNotice.setVisibility(View.VISIBLE);
        if (pageNum == 1) {
            mList.clear();
        }
        mList.addAll(dataBean.list);
        if (pageNum >= dataBean.pages) {
            isEnd = true;
        } else {
            isEnd = false;
            pageNum += 1;
        }
        mAdapter.setNewData(mList);
        mAdapter.loadMoreComplete();
        mSwipeRefreshLayout.setEnabled(true);
    }

    @Override
    public void loadNoticeListFailure(String msg) {
        mProgressBar.setVisibility(View.GONE);
        mSwipeRefreshLayout.setRefreshing(false);
        mRecyclerNotice.setVisibility(View.VISIBLE);
    }
}
